<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_user_detail extends user_detail {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 获取用户详情信息 Api
     */
    public function do_detail() {
        global $_G;
        
        $user_id = isset($_G['user_id']) ? $_G['user_id'] : 0;
        $appid = isset($_REQUEST['appid']) ? $_REQUEST['appid'] : 0;
        
        $return['code'] = "1";
        
        $detail = $this->one_info('user_detail', 'user_id', $user_id);
        
        if(!empty($detail) && is_array($detail)) {
            $return['code'] = "0";
            
            //2017-6-13 11:00:31 通过用户的class_id去class_main表中查找对应的grade_num和class_num
            $grade_num = 0;
            $class_num = 0;
            
            if($detail['class_id']){
                $classInfo = $this->one_info('class_main', 'class_id', $detail['class_id']);
                
                $grade_num = $classInfo['grade_num'];
                $class_num = $classInfo['class_num'];
            }
            
            $detail['grade_num'] = $grade_num;
            $detail['class_num'] = $class_num;
            
            $tmp1 = $this->get_field_value_by_id('user', 'user_id', $detail['user_id'], 'user_name');
            $detail['user_name'] = $tmp1['user_name'];
            $tmp2 = $this->get_field_value_by_id('school_main', 'sm_id', $detail['sm_id'], 'sm_name, sm_type');
            $detail['sm_name'] = $tmp2['sm_name'];
            $detail['sm_type'] = $tmp2['sm_type'];
            
            $detail['user_avatar'] = $detail['user_avatar'] ? 'http://fstore.jxzhjy.com'.$detail['user_avatar'] : 'http://fstore.jxzhjy.com/data/upload/user_avatar.png';
            
            if($appid){
                $return['data']['user_detail'] = $detail;
            }
            else{
                $return['data'] = $detail;   
            }
        }
        
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        return false;
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $user_detail = isset($_REQUEST['user_detail']) && !empty($_REQUEST['user_detail']) ? $_REQUEST['user_detail'] : '';
        if ('1' == $issubmit) {
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach ($user_detail AS $key => $value) {
                if (is_array($value) && !empty($value)) {
                    $user_detail[$key] = implode(",", $value);
                }
            }
            $user_detail['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('user_detail', $user_detail);
            if ($insert_id) {
                showmessage('操作成功', 'index.php?mod=user&action=detail&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=user&action=detail&do=index');
            }
        } else {
            include template('user/detail/add');
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        //user_avatar
        //user_nickname
        //gender
        //user_sign
        //sm_id
        $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : $_G['user_id'];
        $user_detail = isset($_REQUEST['user_detail']) && !empty($_REQUEST['user_detail']) ? $_REQUEST['user_detail'] : '';
        $effect_row = DB::update('user_detail', $user_detail, array('user_id' => $user_id));
        if ($effect_row && !empty($user_id)) {
            $return['code'] = '0';
        } else {
            $return['code'] = '1'; //错误抛异常处理屏蔽 单输出 code=1
        }
        if ($_G['gp_api']) {
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
        if ($user_id) {
            $effect_row = $this->delete("user_detail", array("user_id" => $user_id), $limit = 1);
            if ($effect_row) {
                showmessage('操作成功', 'index.php?mod=user&action=detail&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=user&action=detail&do=index');
            }
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql = " AND user_nickname LIKE '%" . $keyword . "%' ";
        }
        $orderby = " ORDER BY user_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('user_detail', $page_condition);
        $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=detail&do=index&keyword=" . $keyword);
        include template('user/detail/index');
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY user_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('user_detail', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>